﻿using System;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace com.susi.util.toolkit {
    public class PoolAdapter<T> : IPool<T> {
        private Action<T> add;
        private Action<T> remove;

        public PoolAdapter([NotNull] Action<T> add, [NotNull] Action<T> remove)
        {
            this.add    = add ?? throw new ArgumentNullException(nameof(add));
            this.remove = remove ?? throw new ArgumentNullException(nameof(remove));
        }

        public void Add(T item)
        {
            add(item);
        }

        public void Remove(T item)
        {
            remove(item);
        }

        public static PoolAdapter<T> FromCollection(ICollection<T> collection)
        {
            return new PoolAdapter<T>(collection.Add, v => collection.Remove(v));
        }
        
    }
}